#encoding=utf8

import RepairLostValue
from numpy import *

'''
数据归一化：把数据的值转化为0-1的区间值
公式：(oldValue - minValue)/(maxValue - minValue)
'''

#按列数据归一化
def normalize_data_bycol(dataset):
	#0代表取列的最小值,1代表取行的最小值
	min_v = dataset.min(0)
	max_v = dataset.max(0)
	diff_v = max_v - min_v #计算出差值

	res = zeros(dataset.shape)
	rows = dataset.shape[0]

	#分子,dataset为oldValue，tile是复制了一个相同大小的最小值矩阵
	molecular = dataset - tile(min_v,(rows,1))
	#分母
	Denominator = tile(diff_v,(rows,1))
	
	res = molecular/Denominator

	return res


def normalize_data_byrow(dataset):
	min_v = dataset.min(1)
	max_v = dataset.max(1)
	diff_v = max_v - min_v

	res = zeros(dataset.shape)
	cols = dataset.shape[1]

	#分子,dataset为oldValue，tile是复制了一个相同大小的最小值矩阵
	molecular = dataset - tile(min_v,(1,cols))
	#分母
	Denominator = tile(diff_v,(1,cols))
	
	res = molecular/Denominator
	print(res)


if __name__ == '__main__':
	path = r'../new_txt/dataset.data'
	dataset = RepairLostValue.load_dataset(path,'    ')
	dataset = RepairLostValue.repair_lostdata_avg_pandas(dataset)
	normalize_data_byrow(dataset[:-1,:])